'''
This file is part of DopplerShift.

    DopplerShift is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DopplerShift is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DopplerShift.  If not, see <http://www.gnu.org/licenses/>.
'''

from dopplershift.core.exc import DSException
from dopplershift.core.patterns.singleton import DSSingleton

class MediatorServiceRegistered(DSException):
    '''
    Service currently registered
    '''

class MediatorServiceNotFound(DSException):
    '''
    Service not found in mediator
    '''

class DSMediator(object):
    def __init__(self):
        '''
        Mediator creation
        '''
        self._mediated_objects = {}
    def unregister(self, service_id):
        if service_id not in self._mediated_objects:
            raise MediatorServiceNotFound(service_id)
        self._mediated_objects.pop(service_id)
        return self._mediated_objects[service_id]
    def register(self, service_id, service_object):
        if service_id in self._mediated_objects:
            raise MediatorServiceRegistered(service_id)
        self._mediated_objects[service_id] = service_object
        return service_id
    def get(self, service_id):
        if service_id not in self._mediated_objects:
            raise MediatorServiceNotFound(service_id)
        return self._mediated_objects[service_id]
    
class DSMediatorSingleton(DSSingleton):
    obj = None
    class_object = DSMediator

def getMediator():
    return DSMediatorSingleton().get()